package com.hry.store.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hry.store.common.Const;
import com.hry.store.common.ServerResponse;
import com.hry.store.pojo.HotProduct;
import com.hry.store.pojo.ProductColor;
import com.hry.store.pojo.ProductVersion;
import com.hry.store.pojo.SecondCategory;
import com.hry.store.redis.RedisComponent;
import com.hry.store.service.*;
import com.hry.store.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class ProductController {
    @Autowired
    private SecondCategoryService secondCategoryService;
    @Autowired
    private ProductVersionService productVersionService;
    @Autowired
    private ProductColorService productColorService;
    @Autowired
    private ProductVoService productVoService;
    @Autowired
    private HotProductService hotProductService;
    @Autowired
    private RedisComponent redis;

    /**
     * 跳转到产品详情页面
     */
    @RequestMapping("page/product/detail")
    public String pageProductDetail() {

        return "product_detail";
    }

    /**
     * 产品详情
     */
    @RequestMapping("product/detail")
    public String productDetail(@RequestParam("secondCategoryId") Integer secondCategoryId, ModelMap modelMap) {
        // 获取产品名称
        SecondCategory product = secondCategoryService.getProductBySecondCategoryId(secondCategoryId);
        modelMap.put("product", product);
        // 获取版本信息
        List<ProductVersion> productVersionList = productVersionService.getProductVersionBySecondCategoryId(secondCategoryId);
        modelMap.put("productVersionList", productVersionList);

        return "product_detail";
    }

    /**
     * 热门产品详情
     */
    @RequestMapping("hot_product/detail")
    public String hotProductDetail(@RequestParam("secondCategoryId") Integer secondCategoryId, ModelMap modelMap) {
        // 获取产品名称
        HotProduct product = hotProductService.getHotProduct(secondCategoryId);
        modelMap.put("product", product);
        // 获取版本信息
        List<ProductVersion> productVersionList = productVersionService.getProductVersionBySecondCategoryId(secondCategoryId);
        modelMap.put("productVersionList", productVersionList);

        return "product_detail";
    }

    /**
     * 根据产品id版本信息id查询颜色
     */
    @RequestMapping("product/get_product_color_by_productId_and_versionId")
    @ResponseBody
    public ServerResponse getProductColorByProductIdAndVersionId(@RequestParam("secondCategoryId") Integer secondCategoryId, @RequestParam("versionId") Integer versionId) {
        List<ProductColor> productColorList = productColorService.getProductColorBySecondCategoryIdAndVersionId(secondCategoryId, versionId);
        return ServerResponse.createBySuccess(productColorList);
    }

    /**
     * 查询图片信息
     */
    @RequestMapping("product/getImagesByColorName")
    @ResponseBody
    public ServerResponse getProductImages(@RequestParam("secondCategoryId") Integer secondCategoryId,
                                           @RequestParam("versionId") Integer versionId,
                                           @RequestParam("colorName") String colorName) {
        List list = productColorService.getProductImgs(secondCategoryId, versionId, colorName);
        return ServerResponse.createBySuccess(list);
    }

    /**
     * 根据产品id查询产品详细信息
     */
    @RequestMapping("product/getBySecondCategoryId")
    public String getBySecondCategoryId(@RequestParam("secondCategoryId") Integer secondCategoryId, ModelMap modelMap) {

        // 根据产品id查询产品详情
        List<ProductVo> productVoList = productVoService.getBySecondCategoryId(secondCategoryId);
        if (!CollectionUtils.isEmpty(productVoList)) {
            modelMap.put("productVoList", productVoList);
            return "liebiao";
        }
        return null;
    }

    /**
     * 鼠标悬停填充  redis 缓存
     */
    @RequestMapping("product/mouseover")
    @ResponseBody
    public ServerResponse getByTopCategoryId(@RequestParam("topCategory") Integer topCategory) {
        // 先去redis中取数据
        String jsonProduct = redis.sentinelGet(Const.LEFT_PRODUCT + String.valueOf(topCategory));
        if (jsonProduct.equals("null")) {
            List<ProductVo> productVoList = productVoService.getTopCategoryId(topCategory);
            String objectJson = JSON.toJSONString(productVoList);
            String centProduct = Const.LEFT_PRODUCT + String.valueOf(topCategory);
            redis.sentinelSet(centProduct, objectJson);
            return ServerResponse.createBySuccess(productVoList);
        }
        List<ProductVo> productVoList = JSONArray.parseArray(jsonProduct, ProductVo.class);
        return ServerResponse.createBySuccess(productVoList);
    }

    /**
     * 鼠标悬停填充中间版块  redis 缓存
     */
    @RequestMapping("product/getCenter")
    @ResponseBody
    public ServerResponse getCenterByTopCategoryId(@RequestParam("topCategory") Integer topCategory) {
        // 先去redis中取数据
        String jsonProduct = redis.sentinelGet(Const.CENT_PRODUCT + String.valueOf(topCategory));
        if (jsonProduct.equals("null")) {
            List<ProductVo> productVoList = productVoService.getCenterProduct(topCategory);
            String objectJson = JSON.toJSONString(productVoList);
            String centProduct = Const.CENT_PRODUCT + String.valueOf(topCategory);
            redis.sentinelSet(centProduct, objectJson);
            return ServerResponse.createBySuccess(productVoList);
        }
        List<ProductVo> productVoList = JSONArray.parseArray(jsonProduct, ProductVo.class);
        return ServerResponse.createBySuccess(productVoList);

    }

}
